package com.fy.servlet;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import com.fy.model.Stock;
import com.google.gson.Gson;

@WebServlet("/stock")
public class StockServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
	IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		Integer id[] = { 600816, 600817, 600818, 600821, 600822, 600823, 600824, 600825, 600827, 600831, 600832,
				600833, 600836, 600851, 600866 };
		String name[] = { "美锦能源", "华菱星马", "深圳惠程", "金亚科技", "中国国航", "振芯科技", "首钢股份", "上海普天", "丽珠集团", "长江通信", "永泰能源",
				"泰山石油", "太龙药业", "华铁科技", "深圳惠程" };
		Random r = new Random();
		List<Stock> stocks = new ArrayList<Stock>();
		for (int i = 0; i < 15; i++) {
			Stock stock = new Stock();
			stock.setId(id[i]);
			stock.setName(name[i]);
			DecimalFormat df = new DecimalFormat("0.00");
			Double d = Double.parseDouble(df.format(r.nextDouble() * 100));
			stock.setPrice(d);
			stocks.add(stock);
		}
		Gson gson = new Gson();
		String str = gson.toJson(stocks);
		response.getWriter().print(str);
	}
}